﻿using System;
using System.Collections.Generic;

using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.Common.Exceptions;

namespace Fuse8.DomainFramework.BusinessService
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BusinessServiceTransaction<T> : IDisposable 
        where T : class, IEntityObject, new()
    {
        #region Fields

        private bool _isDisposed = false;

        private bool _isTransactionActive = false;

        private IUnitOfWorkFactory<T> _factory;
        private IList<IUnitOfWorkFactory> _slaveFactories;

        #endregion       
        
        #region Constructor

        /// <summary>
        /// 
        /// </summary>
        public BusinessServiceTransaction()
        {
            _factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<T>>();

            if (_factory == null)
            {
                throw new UnitOfWorkFactoryNotDefinedException();
            }

            var currentUnitOfWork = _factory.Get(true);

            if (currentUnitOfWork == null)
            {
                throw new UnitOfWorkNotDefinedException();
            }

            var unitOfWorkScope = new UnitOfWorkScope<T>();

            _isTransactionActive = true;
        } 

        #endregion

        #region Methods

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TSlave"></typeparam>
        /// <returns></returns>
        public BusinessServiceTransaction<T> With<TSlave>()
            where TSlave : class, IEntityObject, new()
        {
            var slaveFactory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<TSlave>>();

            if (slaveFactory == null)
            {
                throw new UnitOfWorkFactoryNotDefinedException();
            }

            if (_slaveFactories == null)
            {
                _slaveFactories = new List<IUnitOfWorkFactory>();
            }

            _slaveFactories.Add(slaveFactory);

            slaveFactory.UseFrom(_factory.Get(true));

            var unitOfWorkScope = new UnitOfWorkScope<TSlave>();

            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Commit()
        {
            if (_isTransactionActive)
            {
                _factory.CommitTransaction();

                _isTransactionActive = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Rollback()
        {
            if (_isTransactionActive)
            {
                _factory.RollbackTransaction();

                _isTransactionActive = false;
            }
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// 
        /// </summary>
        ~BusinessServiceTransaction()
        {
            Dispose(false);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                if (disposing)
                {
                    Rollback();

                    _factory.Close();

                    _factory = null;

                    if (_slaveFactories != null)
                    {
                        foreach (var item in _slaveFactories)
                        {
                            item.Close();
                        }
                        _slaveFactories = null;
                    }
                }
            }

            _isDisposed = true;
            _isTransactionActive = false;
        }
        
        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            Dispose(true);

            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
